﻿Public Class clsCopyMarketingDocument

    Public Function CopyDocument(ByVal oBaseType As SAPbobsCOM.BoObjectTypes, _
                     ByVal oBaseEntry As Integer, ByVal oTarType As SAPbobsCOM.BoObjectTypes, _
                     ByRef oDICompany As SAPbobsCOM.Company, ByRef sErrDesc As String, ByRef IsDraft As Boolean) As Long

        Dim lRetCode As Double
        Dim sFuncName As String = String.Empty
        Dim oBaseDoc As SAPbobsCOM.Documents = _
              oDICompany.GetBusinessObject(oBaseType)
        Dim oTarDoc As SAPbobsCOM.Documents = _
                   oDICompany.GetBusinessObject(oTarType)
        Try
            sFuncName = "CopyDocument()"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting Function", sFuncName)


          
            If oBaseDoc.GetByKey(oBaseEntry) Then
                'base document found, copy to target doc

                If IsDraft = False Then
                    oTarDoc = oDICompany.GetBusinessObject(oTarType)
                Else
                    oTarDoc = oDICompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)
                    oTarDoc.DocObjectCode = oTarType
                End If

               
                'todo: copy the cardcode, docduedate, lines
                oTarDoc.CardCode = oBaseDoc.CardCode
                oTarDoc.DocDueDate = oBaseDoc.DocDueDate
                oTarDoc.DocDate = oBaseDoc.DocDate
                oTarDoc.TaxDate = oBaseDoc.TaxDate
                oTarDoc.NumAtCard = oBaseDoc.NumAtCard
                oTarDoc.Project = oBaseDoc.Project
                oTarDoc.UserFields.Fields.Item("U_POSNo").Value = oBaseDoc.UserFields.Fields.Item("U_POSNo").Value
                oTarDoc.UserFields.Fields.Item("U_Covers").Value = oBaseDoc.UserFields.Fields.Item("U_Covers").Value


                'copy the lines
                Dim count As Integer = oBaseDoc.Lines.Count - 1
                Dim oTargetLines As SAPbobsCOM.Document_Lines = oTarDoc.Lines
                For i As Integer = 0 To count
                    If i <> 0 Then
                        oTargetLines.Add()
                    End If
                    oTargetLines.BaseType = oBaseType
                    oTargetLines.BaseEntry = oBaseEntry
                    oTargetLines.BaseLine = i
                Next

                lRetCode = oTarDoc.Add()

                If lRetCode <> 0 Then

                    sErrDesc = oDICompany.GetLastErrorDescription
                    Call WriteToLogFile(sErrDesc, sFuncName)
                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
                    Return RTN_ERROR
                Else
                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS ", sFuncName)
                    Return RTN_SUCCESS
                End If

            Else
                If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Base Document Not Found ", sFuncName)
                Return RTN_SUCCESS
            End If
        Catch ex As Exception
            sErrDesc = ex.Message
            Call WriteToLogFile(sErrDesc, sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
            Return RTN_ERROR

        Finally
            oTarDoc = Nothing
            oBaseDoc = Nothing

        End Try
    End Function

    Public Function CovertDraftToActualDocuent(ByVal oDICompany As SAPbobsCOM.Company, ByVal sDocEntry As String) As Boolean
        Try
            Dim oDraft As SAPbobsCOM.Documents
            Dim oGRPO As SAPbobsCOM.Documents
            Dim oRS As SAPbobsCOM.Recordset
            Dim lRetCode As Long
            Dim sErrMsg As String = String.Empty

            'Dim sqlStr As String
            Dim i As Integer


            oDraft = oDICompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)
            oRS = oDICompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

            'sqlStr = "SELECT DocEntry" & _
            '         "  FROM ODRF" & _
            '         " WHERE DocStatus = 'O'" & _
            '         "   AND DataSource = 'O'" & _
            '         "   AND ObjType = 13 and "
            'oRS.DoQuery(sqlStr)

            ''  While Not (oRS.EoF)
            If oDraft.GetByKey(CInt(sDocEntry)) Then
                'Build the GRPO (new GRPO Obj for each GRPO Doc)
                oGRPO = oDICompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oInvoices)

                oGRPO.DocDate = oDraft.DocDate
                oGRPO.DocDueDate = oDraft.DocDueDate
                oGRPO.DiscountPercent = oDraft.DiscountPercent
                oGRPO.CardCode = oDraft.CardCode

                For i = 0 To oDraft.Lines.Count - 1
                    oDraft.Lines.SetCurrentLine(i)

                    oGRPO.Lines.ItemCode = oDraft.Lines.ItemCode
                    oGRPO.Lines.Quantity = oDraft.Lines.Quantity
                    oGRPO.Lines.UnitPrice = oDraft.Lines.UnitPrice
                    oGRPO.Lines.LineTotal = oDraft.Lines.LineTotal
                    oGRPO.Lines.TaxCode = oDraft.Lines.TaxCode
                    oGRPO.Lines.CostingCode = oDraft.Lines.CostingCode
                    oGRPO.Lines.COGSAccountCode = oDraft.Lines.COGSAccountCode
                    oGRPO.Lines.COGSCostingCode = oDraft.Lines.COGSCostingCode
                    oGRPO.Lines.WarehouseCode = oDraft.Lines.WarehouseCode

                    'oGRPO.Lines.BaseType = oDraft.Lines.BaseType
                    'oGRPO.Lines.BaseEntry = oDraft.Lines.BaseEntry
                    ''oGRPO.Lines.BaseLine = oDraft.Lines.LineNum

                    If i < oDraft.Lines.Count - 1 Then
                        oGRPO.Lines.Add()
                    End If

                Next i

                'Add the GRPO
                lRetCode = oGRPO.Add
                If lRetCode <> 0 Then

                    oDICompany.GetLastError(lRetCode, sErrMsg)
                    ''Log.WriteLog("Error Adding GRPO : " & sErrMsg, EventLogEntryType.Warning)
                    Return False
                End If

                'Kill the GRPO
                oGRPO = Nothing

                'Remove the Draft
                oDraft.Remove()

            End If

            'Move to the next record
            ''  oRS.MoveNext()
            ''End While

            Return True
        Catch ex As Exception

        End Try
    End Function

End Class
